const noContentToast = Swal.mixin({
    toast: true,
    position: 'center',
    background: '#ffffe1',
    showConfirmButton: false,
    backdrop: false,
    timer: 3000
});

function noContentSearchOnAuthorButtonClick() {
    var authorText = $('#author-toggle-button').text();

    if (authorText == '报告缺失的作者名') {
        $('#author-name-container').css('display', 'block');
        $('#author-toggle-button').text('隐藏作者');
    }
    else {
        $('#author-name-container').css('display', 'none');
        $('#author-toggle-button').text('报告缺失的作者名');
    }
}

function noContentSearchOnBookButtonClick() {
    var bookText = $('#book-toggle-button').text();

    if (bookText == '报告缺失的书名') {
        $('#book-name-container').css('display', 'block');
        $('#book-toggle-button').text('隐藏书名');
    }
    else {
        $('#book-name-container').css('display', 'none');
        $('#book-toggle-button').text('报告缺失的书名');
    }
}

function noContentSearchOnAuthorSubmit() {
    var name = $('#lost-author-name').val();

    if (name.trim() == '') {
        noContentToast.fire({
            type: 'error',
            title: '沒有提供作者名',
        });
        return;
    }

    $.ajax({
        type: 'post',
        async: true,
        data: {'name': name},
        url: document.location.origin + '/api/submitlostauthor',
        dataType: 'text',
        error: function() {

        },
        success: function(data) {
            var json = JSON.parse(data);

            if (json.result == 200) {
                noContentToast.fire({
                    type: 'success',
                    title: '提交成功。感谢您的付出',
                });

                $('#lost-author-name').val('');
            }
            else {
                noContentToast.fire({
                    type: 'error',
                    title: json.description,
                });
            }
        }
    });
}

function noContentSearchOnBookSubmit() {
    var name = $('#lost-book-name').val();

    if (name.trim() == '') {
        noContentToast.fire({
            type: 'error',
            title: '沒有提供书名',
        });
        return;
    }

    $.ajax({
        type: 'post',
        async: true,
        data: {'name': name},
        url: document.location.origin + '/api/submitlostbook',
        dataType: 'text',
        error: function() {

        },
        success: function(data) {
            var json = JSON.parse(data);

            if (json.result == 200) {
                noContentToast.fire({
                    type: 'success',
                    title: '提交成功。感謝您的付出',
                });

                $('#lost-book-name').val('');
            }
            else {
                noContentToast.fire({
                    type: 'error',
                    title: json.description,
                });
            }
        }
    });
}